/**
 * created by itwangyang on 2020-7-27
 * 说明：配置request请求
 */

// 1.引入axios
import axios from "axios"
// 7.引入qs
import qs from 'qs';

// 8.引入local.js
import local from '@/utils/local'
// import {
//   config
// } from "vue/types/umd";

// 5.引入element-ui的message
import {
  Message
} from 'element-ui'

// 2.配置地址
axios.defaults.baseURL = 'http://127.0.0.1:8000';
// import {
//   from
// } from "core-js/fn/array";
// import {
//   resolve,
//   reject
// } from "core-js/fn/promise";

// 3.配置请求超时时间 1000ms = 1s
axios.defaults.timeout = 5000;


// 3.1.配置请求头
axios.interceptors.request.use((config) => {
  let token = local.get('t_k') || ''; // 取出本地token
  if (token) {
    config.headers.Authorization = token
  }
  return config
})



// 4.配置请求拦截
axios.interceptors.response.use(response => {
  console.log(response)
  // 如果有code和message
  if (response.data.code !== "undefined" && response.data.msg !== "undefined") {
    // 去除code和msg
    let {
      code,
      msg
    } = response.data
    if (code === 0) {
      // 成功样式的提示框
      Message({
        type: 'success',
        message: msg
      })
    } else if (code === 1) {
      Message.error(msg)
    }
  }
  return response;
})

// 6.配置通过的get和post请求方法
export default {
  // get
  get(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      }).then((response) => {
        // 成功的回调
        resolve(response.data)
      }).catch((err) => {
        // 失败的回调
        reject(err)
      })
    })
  },
  // post
  post(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios.post(url, qs.stringify(params)).then((response) => {
        resolve(response.data)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}


import Axios from 'axios'

